﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Lookup
{
    public partial class TreeSelect : Page
    {
        private String _sqlDataSelect = "";
        private String _NameField = "";
        private String _ValueField = "";
        private String _RelationField = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["SQLID"] != null)
                {
                    _sqlDataSelect = Session[Request.Params["SQLID"]].ToString();
                }
                if (Request.Params["NameField"] != null)
                {
                    _NameField = Request.Params["NameField"];
                }
                if (Request.Params["ValueField"] != null)
                {
                    _ValueField = Request.Params["ValueField"];
                }
                if (Request.Params["RelationField"] != null)
                {
                    _RelationField = Request.Params["RelationField"];
                }

                InitTree();
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            string returnvalue = "";

            TreeNodeCollection selectNodes = TreeView_DataList.CheckedNodes;
            for (int iIndex = 0; iIndex < selectNodes.Count; iIndex++)
            {
                if (selectNodes[iIndex].ChildNodes.Count > 0)
                {
                    continue;
                }
                if (returnvalue != "")
                {
                    returnvalue += ";";
                }
                returnvalue += selectNodes[iIndex].Text + "|" +selectNodes[iIndex].Value;
            }

            string mfstr = "<script>\n";
            mfstr += "window.returnValue='" + returnvalue.Replace("'", "\\'") + "';\n";
            mfstr += "window.close();\n";
            mfstr += "</script>\n";
            if (!Page.ClientScript.IsClientScriptBlockRegistered("mfscript"))
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "mfscript", mfstr);
            }
        }
        private void InitTree()
        {
            System.Data.DataTable dtData = new CommonBll().getDataFromSQL(_sqlDataSelect);
            if (dtData != null && dtData.Rows.Count > 0)
            {
                TreeViewSet(TreeView_DataList, _sqlDataSelect, _NameField, _ValueField, _RelationField, "");
            }
        }
        /// <summary>
        /// 设置树形结构
        /// </summary>
        /// <param name="tree">树控件</param>
        /// <param name="sqlTreeData">树结构数据选择ＳＱＬ</param>
        private void TreeViewSet(TreeView tree, String sqlTreeData, String nameField, String valueField, String relationField, String linkField)
        {
            tree.Nodes.Clear();
            System.Data.DataTable dtData = new DataTable();
            dtData = new  CommonBll().getDataFromSQL(sqlTreeData);         
            CreateTree(tree, dtData, nameField, valueField, relationField, linkField);
        }

        /// <summary>
        /// 创建树形结构
        /// </summary>
        /// <param name="treeData">树结构数据</param>
        /// <param name="nameField">节点名字段</param>
        /// <param name="valueField">节点值字段</param>
        /// <param name="relationField">节点关系字段</param>
       private void CreateTree(TreeView tree, System.Data.DataTable treeData, String nameField, String valueField, String relationField, String linkField)
        {
            if (treeData != null && treeData.Columns.Contains(nameField) && treeData.Columns.Contains(valueField) && treeData.Columns.Contains(relationField))
            {
                System.Data.DataRow[] drFirstLevel = treeData.Select(relationField + " IS NULL");

                if (drFirstLevel.Length > 0)
                {
                    for (int iIndex = 0; iIndex < drFirstLevel.Length; iIndex++)
                    {
                        TreeNode childNode = CreateTreeNode(treeData, nameField, valueField, relationField, drFirstLevel[iIndex][valueField].ToString(), linkField);
                        if (childNode != null)
                        {
                            tree.Nodes.Add(childNode);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 创建树节点
        /// </summary>
        /// <param name="treeData">树结构数据</param>
        /// <param name="nameField">节点名字段</param>
        /// <param name="valueField">节点值字段</param>
        /// <param name="relationField">节点关系字段</param>
        /// <param name="currentLevel">当前节点</param>
        /// <returns></returns>
        private TreeNode CreateTreeNode(System.Data.DataTable treeData, String nameField, String valueField, String relationField, String currentLevel, String linkField)
        {
            TreeNode rootNode = null;

            System.Data.DataRow[] drRoot = treeData.Select(valueField + "='" + currentLevel + "'");
            if (drRoot.Length > 0)
            {
                //生成当前节点
                if (linkField != null && linkField != "")
                {
                    rootNode = new TreeNode(drRoot[0][nameField].ToString(), drRoot[0][valueField].ToString(), "", drRoot[0][linkField].ToString(), "_self");
                    if (drRoot[0][linkField].ToString() == "")
                    {
                        rootNode.SelectAction = TreeNodeSelectAction.None;
                    }
                }
                else
                {
                    rootNode = new TreeNode(drRoot[0][nameField].ToString(), drRoot[0][valueField].ToString());
                    rootNode.SelectAction = TreeNodeSelectAction.None;
                }

                System.Data.DataRow[] drCurrentNode = treeData.Select(relationField + "='" + currentLevel + "'");
                if (drCurrentNode.Length > 0)
                {
                    for (int iIndex = 0; iIndex < drCurrentNode.Length; iIndex++)
                    {
                        //创建子节点
                        TreeNode childNode = CreateTreeNode(treeData, nameField, valueField, relationField, drCurrentNode[iIndex][valueField].ToString(), linkField);
                        if (childNode != null)
                        {
                            rootNode.ChildNodes.Add(childNode);
                        }
                    }
                }
            }
            return rootNode;
        }
    }
}
